package com.rongji.dfish.hello.controller;

import com.rongji.dfish.base.Pagination;
import com.rongji.dfish.framework.mvc.controller.BaseActionController;
import com.rongji.dfish.framework.mvc.response.JsonResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RequestMapping("/hello")
@Controller
public class HelloController extends BaseActionController {

    @RequestMapping("/submit")
    @ResponseBody
    public Object submit(HttpServletRequest request) {
        return new JsonResponse<>(request.getParameterMap());
    }


    @RequestMapping("/ajax")
    @ResponseBody
    public Object ajax(HttpServletRequest request) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");

        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("id", id);
        dataMap.put("name", name);

        return new JsonResponse<>(dataMap);
    }

    @RequestMapping("/template/hello")
    @ResponseBody
    public Object templateHello(HttpServletRequest request) {
//        return new JsonResponse<>("后台动态页面数据2");
        return "{\"myString\":\"这是我定义的json格式\"}";
    }

    @RequestMapping("/template/table")
    @ResponseBody
    public Object templateTable(HttpServletRequest request) {
//        List<String[]> dataList = Arrays.asList(
//                new String[]{"张三", "一班"},
//                new String[]{"李四", "一班"},
//                new String[]{"王五", "一班"},
//                new String[]{"赵六", "二班"},
//                new String[]{"孙七", "三班"}
//                );
        List<User> userList = Arrays.asList(
                new User("03", "张三", "一班"),
                new User("04", "李四", "一班"),
                new User("05", "王五", "二班"),
                new User("06", "赵六", "二班"),
                new User("07", "孙七", "三班")
        );

        Pagination pagination = new Pagination();
        pagination.setOffset(10);
        pagination.setLimit(10);
        pagination.setSize(31);

        return new JsonResponse<>(userList).setPagination(pagination);
    }

    private static class User {
        private String userId;
        private String userName;
        private String orgId;
        private String orgName;
        private String gender;
        private Date birthday;
        private String resume;

        public User(String userId, String userName, String orgName) {
            this.userId = userId;
            this.userName = userName;
            this.orgName = orgName;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getOrgId() {
            return orgId;
        }

        public void setOrgId(String orgId) {
            this.orgId = orgId;
        }

        public String getOrgName() {
            return orgName;
        }

        public void setOrgName(String orgName) {
            this.orgName = orgName;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }

        public String getResume() {
            return resume;
        }

        public void setResume(String resume) {
            this.resume = resume;
        }
    }

    @RequestMapping("/template/children")
    @ResponseBody
    public Object templateChildren(HttpServletRequest request) {
        String parent = request.getParameter("parent");
        return new JsonResponse<>(regionMap.get(parent));
    }

    private static Map<String, Region> regions = new HashMap<>();
    private static Map<String, List<Region>> regionMap = new HashMap<>();

    static {
        List<Region> regionList = new ArrayList<>();
        regionList.add(new Region("110000000000", "北京市", "ROOT"));
        regionList.add(new Region("120000000000", "天津市", "ROOT"));
        regionList.add(new Region("310000000000", "上海市", "ROOT"));
        regionList.add(new Region("500000000000", "重庆市", "ROOT"));
        regionList.add(new Region("350000000000", "福建省", "ROOT"));

        regionList.add(new Region("350100000000", "福州市", "350000000000"));
        regionList.add(new Region("350200000000", "厦门市", "350000000000"));
        regionList.add(new Region("350300000000", "莆田市", "350000000000"));
        regionList.add(new Region("350400000000", "三明市", "350000000000"));
        regionList.add(new Region("350500000000", "泉州市", "350000000000"));
        regionList.add(new Region("350600000000", "漳州市", "350000000000"));
        regionList.add(new Region("350700000000", "南平市", "350000000000"));
        regionList.add(new Region("350800000000", "龙岩市", "350000000000"));
        regionList.add(new Region("350900000000", "宁德市", "350000000000"));


        regionList.add(new Region("350101000000", "市辖区", "350100000000"));
        regionList.add(new Region("350102000000", "鼓楼区", "350100000000"));
        regionList.add(new Region("350103000000", "台江区", "350100000000"));
        regionList.add(new Region("350104000000", "仓山区", "350100000000"));
        regionList.add(new Region("350105000000", "马尾区", "350100000000"));
        regionList.add(new Region("350111000000", "晋安区", "350100000000"));
        regionList.add(new Region("350112000000", "长乐区", "350100000000"));
        regionList.add(new Region("350121000000", "闽侯县", "350100000000"));
        regionList.add(new Region("350122000000", "连江县", "350100000000"));
        regionList.add(new Region("350123000000", "罗源县", "350100000000"));
        regionList.add(new Region("350124000000", "闽清县", "350100000000"));
        regionList.add(new Region("350125000000", "永泰县", "350100000000"));
        regionList.add(new Region("350128000000", "平潭县", "350100000000"));
        regionList.add(new Region("350181000000", "福清市", "350100000000"));

        for (Region region : regionList) {
            regions.put(region.getCode(), region);
            List<Region> subList = regionMap.get(region.getParent());
            if (subList == null) {
                subList = new ArrayList<>();
                regionMap.put(region.getParent(), subList);
            }
            subList.add(region);
            Region parentRegion = regions.get(region.getParent());
            if (parentRegion != null) {
                parentRegion.setSubCount(subList.size());
            }
        }
    }


    private static class Region {
        private String code;
        private String name;
        private String parent;
        private int subCount;

        public Region(String code, String name, String parent) {
            this.code = code;
            this.name = name;
            this.parent = parent;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getParent() {
            return parent;
        }

        public void setParent(String parent) {
            this.parent = parent;
        }

        public int getSubCount() {
            return subCount;
        }

        public void setSubCount(int subCount) {
            this.subCount = subCount;
        }
    }

    @RequestMapping("/template/form")
    @ResponseBody
    public Object templateForm(HttpServletRequest request) {
        String id = request.getParameter("id");
        User user = new User(id, "测试姓名" + id, "一班" + id);
        user.setGender("2");
        user.setBirthday(new Date());
        user.setResume("不告诉你" + id);
        return new JsonResponse<>(user);
    }

}
